import { Router } from "vue-router";
import { useWebStore } from "@/store";

const basicRouter = ["/index","/about","/course", "/exam", "/practice", "/news","/training","/contact", "/chatGPT", "/userCenter"];

/**
 * 动态路由权限控制
 * @param router
 */
export function setupPermissionGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    if (to.meta && to.meta.title) {
      // @ts-ignore
      document.title = to.meta.title; // 设置浏览器标题
    }
    const webStore = useWebStore();
    if (to.path.includes("/userCenter")) {
      webStore.setCurrentModel({
        path: "/userCenter"
      });
    } else {
      const nav = basicRouter.filter((item: any) => to.path.includes(item));
      if (nav) {
        webStore.setCurrentModel({
          path: nav
        });
      }
    }

    //未登录则跳转登录页面，待正式上线需要将其注释
   /* const token = localStorage.getItem("token");
    if (!token && to.path !== "/login") {
      next({
        path: "/login",
        query: { redirect: to.fullPath } // 将要跳转路由的path作为参数，传递到登录页面
      });
      return;
    }*/

    next();
  });
}
